package com.wontube.smr.bo.sites.youtube;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.wontube.client.extension.utils.ArrayUtils;
import com.wontube.client.extension.utils.StringUtils;
import com.wontube.smr.bo.constants.StandarMusicGenres;
import com.wontube.smr.client.model.po.Genre;

public class YoutubeMusicGenres {
	private YoutubeMusicGenres(){}
	
	//网站使用的music genre映射到标准genre
	//private static final Map<String, String> siteStandarMap = new LinkedHashMap<String, String>();	
	private static final Map<String, String> map = new LinkedHashMap<String, String>();	
	private final static List<Genre> i18nGenres = new ArrayList<Genre>();
	
	static{
		try {
			//loadMusicGenresMap();
			mapStandarGenres();
			initI18nGenres();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/*
	private static void loadMusicGenresMap(){
		List<GenreSite> genreSites = GenreSelectDao.selectGenreSites(SiteConstants.WEBSITE_YOUTUBE, 
				Genre.CATE_MUSIC);
		for (GenreSite gs : genreSites) {
			siteStandarMap.put(gs.getSiteGenre(), gs.getStandarGenre());
		}
	}
	*/
	
	private static void mapStandarGenres(){
		map.put("alternative", StandarMusicGenres.ALTERNATIVE);
		map.put("blues", StandarMusicGenres.BLUES);
		map.put("comedy", StandarMusicGenres.COMEDY);
		map.put("country", StandarMusicGenres.COUNTRY);
		map.put("easy", StandarMusicGenres.EASY_LISTENING);
		map.put("electronic", StandarMusicGenres.ELECTRONIC);
		map.put("folk", StandarMusicGenres.FOLK);
		map.put("international", StandarMusicGenres.INTERNATIONAL);
		map.put("jazz", StandarMusicGenres.JAZZ);
		map.put("kpop", StandarMusicGenres.K_POP);
		map.put("latin", StandarMusicGenres.LATIN);
		map.put("metal", StandarMusicGenres.METAL);
		map.put("newage", StandarMusicGenres.NEW_AGE);
		map.put("pop", StandarMusicGenres.POP);
		map.put("r_and_b", StandarMusicGenres.R_AND_B);
		map.put("rap", StandarMusicGenres.RAP);
		map.put("reggae", StandarMusicGenres.REGGAE);
		map.put("religious", StandarMusicGenres.RELIGIOUS);
		map.put("rock", StandarMusicGenres.ROCK);
		map.put("stage", StandarMusicGenres.STAGE_SCREEN);
		map.put("vocal", StandarMusicGenres.VOCAL);
	}
	
	private static void initI18nGenres(){
		i18nGenres.add(new Genre(StandarMusicGenres.ALTERNATIVE, "Alternative"));
		i18nGenres.add(new Genre(StandarMusicGenres.BLUES, "Blues"));
		i18nGenres.add(new Genre(StandarMusicGenres.COUNTRY, "Country"));
		i18nGenres.add(new Genre(StandarMusicGenres.ELECTRONIC, "Electronic"));
		i18nGenres.add(new Genre(StandarMusicGenres.JAZZ, "Jazz"));
		i18nGenres.add(new Genre(StandarMusicGenres.LATIN, "Latin"));
		i18nGenres.add(new Genre(StandarMusicGenres.METAL, "Metal"));
		i18nGenres.add(new Genre(StandarMusicGenres.POP, "Pop"));
		i18nGenres.add(new Genre(StandarMusicGenres.R_AND_B, "R&B"));
		i18nGenres.add(new Genre(StandarMusicGenres.RAP, "Rap"));
		i18nGenres.add(new Genre(StandarMusicGenres.REGGAE, "Reggae"));		
		i18nGenres.add(new Genre(StandarMusicGenres.ROCK, "Rock"));
		//i18nGenres.add(new Genre(StandarMusicGenres.SOUND_TRACK, "Sound Track"));
		i18nGenres.add(new Genre(StandarMusicGenres.OTHERS, StandarMusicGenres.OTHERS));
	}
	
	public static List<Genre> getI18nGenres(){
		return i18nGenres;
	}
	
	public static String[] getI18nOtherGenres(){
		return new String[]{StandarMusicGenres.K_POP, StandarMusicGenres.RELIGIOUS, StandarMusicGenres.EASY_LISTENING,
				StandarMusicGenres.NEW_AGE, StandarMusicGenres.STAGE_SCREEN, StandarMusicGenres.FOLK, 
				StandarMusicGenres.COMEDY, StandarMusicGenres.INTERNATIONAL, StandarMusicGenres.VOCAL};
	}
	
	/**
	 * 修正单个genre
	 * @param genreName
	 * @return
	 */
	public static String remendGenre(String genreName){
		if(StringUtils.isNullOrEmpty(genreName)){
			return null;
		}
		
		//处理专有genre, 不存在则判断是标准genre
		genreName = genreName.trim();
		String genre = map.get(genreName);
		return genre != null ? genre : genreName;		
	}
	
	/**
	 * 修正以逗号分隔的genres
	 * @param genreNames
	 * @return
	 */
	public static String remendAllGenres(String genreNames){
		if(StringUtils.isNullOrEmpty(genreNames)){
			return null;
		}
		
		Set<String> genreSet = new HashSet<String>();
		
		String[] genres = genreNames.split("\\s*,\\s*");
		for (String g : genres) {
			String rg = remendGenre(g);
			if(rg != null){
				genreSet.add(rg);
			}		
		}
		
		return ArrayUtils.join(genreSet, ",");
	}
	
	/**
	 * Youtube内部使用的genres
	 * @return
	 */
	public static String[] getInnerGenres(){
		Set<String> keys = map.keySet();		
		String[] all = new String[keys.size()];
		int round = 0;
		for (String key : keys) {
			all[round++] = key;
		}
		return all;
	}
}
